--[[
This code is MIT licensed, see http://www.opensource.org/licenses/mit-license.php
(C) 2010 - 2011 Gideros Mobile 
--]]

Tree = Core.class(Target)


function Tree:init(list,shottedList)
	self:setFrame(list)
	self:setShottable(false)
end

function Tree:setFrame(framesList)
	-- create a Bitmap for each frame
	self.points=5
	self.frames = {}
	for i = 1, #framesList do
		self.frames[i] = Bitmap.new(Texture.new(framesList[i]))
		
	end

	self.nframes = #framesList

	-- add first Bitmap as a child
	self.frame = 1
	self:addChild(self.frames[1])
	
	-- subframe is used to adjust the speed of animation
	self.subframe = 0

	-- set initial position
	self:setPosition(0, 0)

	
	
	
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
	self:addEventListener(Event.TOUCHES_END,self.imagetouch,self)
	
end

function Tree:imagetouch(event)
	
	if self:hitTestPoint(event.touch.x, event.touch.y) then
		local x = event.touch.x
		local y = event.touch.y
		
		self.isTreeShotted=true
	end	
end

function Tree:onEnterFrame()
print(self.isTreeShotted)
	if self.isTreeShotted then
		self.subframe = self.subframe + 1
	
		-- for every 10 frames, we remove the old frame and add the new frame as a child
		if self.subframe > 10 then
			print(self.frame)
			
			
			self:removeChild(self.frames[self.frame])

			self.frame = self.frame + 1
			if self.frame > self.nframes then
				self.frame = 1
				self.isTreeShotted=false
			end

			self:addChild(self.frames[self.frame])

			self.subframe = 0
		end
	
	
	end
	-- if the bird is out of the screen, set new speed and position
	
end
